

import 'package:flutter_hms_scan_kit/flutter_hms_scan_kit.dart';
import 'package:flutter_hms_scan_kit/scan_result.dart';
import 'package:get/get.dart';
import 'package:hygf/config/GlobalConfig.dart';
import 'package:hygf/http/file_transfer/down_cap.dart';
import 'package:hygf/util/location/location_gaode.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../config/ApiConfig.dart';
import '../../../../config/RouteConfig.dart';
import '../../../../http/DioClient.dart';
import '../../../../http/core/ResponseData.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../view/image_picker_widget.dart';
import '../../construct/un_construct/logic.dart';
import '../CustomerDetailPage.dart';
import 'state.dart';

class ConstructMsgLogic extends GetxController {
  final ConstructMsgState state = ConstructMsgState();

  void save() {
    submit(0);
  }

  Future<void> submit(int flag) async {
    state.addConstructBean.saveOrSubmit = flag;
    state.addConstructBean.id = state.constructionLink.id;
    state.addConstructBean.customerInfoId =
        state.constructionLink.customerInfoId;
    state.addConstructBean.startDate = state.constructionLink.startDate;
    state.addConstructBean.endDate = state.constructionLink.endDate;
    state.addConstructBean.address = state.constructionLink.address;
    state.addConstructBean.constructionUser =
        state.constructionLink.constructionUser;
    state.addConstructBean.addressLongitude =
        state.constructionLink.addressLongitude.toString();
    state.addConstructBean.addressLatitude =
        state.constructionLink.addressLatitude.toString();
    state.addConstructBean.safetyUser = state.constructionLink.safetyUser;
    state.addConstructBean.componentNumber =
        state.constructionLink.componentNumber;
    state.addConstructBean.materialsEntranceDate =
        state.constructionLink.materialsEntranceDate;
    state.addConstructBean.remarks = state.constructionLink.remarks;
    if (state.mEntrancePhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.materialsEntrancePhoto ?? []).isEmpty) {
        state.addConstructBean.materialsEntrancePhoto = [];
      }
      for (var item in state.mEntrancePhotoController.data) {
        state.addConstructBean.materialsEntrancePhoto!.add(item.id ?? 0);
      }
    }
    if (state.classMeetingPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.classMeetingPhoto ?? []).isEmpty) {
        state.addConstructBean.classMeetingPhoto = [];
      }
      for (var item in state.classMeetingPhotoController.data) {
        state.addConstructBean.classMeetingPhoto!.add(item.id ?? 0);
      }
    }
    if (state.safetyProtectionPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.safetyProtectionPhoto ?? []).isEmpty) {
        state.addConstructBean.safetyProtectionPhoto = [];
      }
      for (var item in state.safetyProtectionPhotoController.data) {
        state.addConstructBean.safetyProtectionPhoto!.add(item.id ?? 0);
      }
    }
    if (state.stentInstallPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.stentInstallPhoto ?? []).isEmpty) {
        state.addConstructBean.stentInstallPhoto = [];
      }
      for (var item in state.stentInstallPhotoController.data) {
        state.addConstructBean.stentInstallPhoto!.add(item.id ?? 0);
      }
    }
    if (state.componentInstallPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.componentInstallPhoto ?? []).isEmpty) {
        state.addConstructBean.componentInstallPhoto = [];
      }
      for (var item in state.componentInstallPhotoController.data) {
        state.addConstructBean.componentInstallPhoto!.add(item.id ?? 0);
      }
    }
    if (state.inverterInstallGroundController.data.isNotEmpty) {
      if ((state.addConstructBean.inverterInstallGround ?? []).isEmpty) {
        state.addConstructBean.inverterInstallGround = [];
      }
      for (var item in state.inverterInstallGroundController.data) {
        state.addConstructBean.inverterInstallGround!.add(item.id ?? 0);
      }
    }
    if (state.stentInstallGroundController.data.isNotEmpty) {
      if ((state.addConstructBean.stentInstallGround ?? []).isEmpty) {
        state.addConstructBean.stentInstallGround = [];
      }
      for (var item in state.stentInstallGroundController.data) {
        state.addConstructBean.stentInstallGround!.add(item.id ?? 0);
      }
    }
    if (state.inverterInstallPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.inverterInstallPhoto ?? []).isEmpty) {
        state.addConstructBean.inverterInstallPhoto = [];
      }
      for (var item in state.stentInstallGroundController.data) {
        state.addConstructBean.inverterInstallPhoto!.add(item.id ?? 0);
      }
    }
    if (state.cableLayingPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.cableLayingPhoto ?? []).isEmpty) {
        state.addConstructBean.cableLayingPhoto = [];
      }
      for (var item in state.cableLayingPhotoController.data) {
        state.addConstructBean.cableLayingPhoto!.add(item.id ?? 0);
      }
    }
    if (state.componentFinishPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.componentFinishPhoto ?? []).isEmpty) {
        state.addConstructBean.componentFinishPhoto = [];
      }
      for (var item in state.componentFinishPhotoController.data) {
        state.addConstructBean.componentFinishPhoto!.add(item.id ?? 0);
      }
    }
    if (state.resistancePhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.resistancePhoto ?? []).isEmpty) {
        state.addConstructBean.resistancePhoto = [];
      }
      for (var item in state.resistancePhotoController.data) {
        state.addConstructBean.resistancePhoto!.add(item.id ?? 0);
      }
    }
    if (state.voltagePhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.voltagePhoto ?? []).isEmpty) {
        state.addConstructBean.voltagePhoto = [];
      }
      for (var item in state.voltagePhotoController.data) {
        state.addConstructBean.voltagePhoto!.add(item.id ?? 0);
      }
    }
    if (state.rnOtherImgController.data.isNotEmpty) {
      if ((state.addConstructBean.other ?? []).isEmpty) {
        state.addConstructBean.other = [];
      }
      for (var item in state.rnOtherImgController.data) {
        state.addConstructBean.other!.add(item.id ?? 0);
      }
    }
    if (state.inverterPhotoController.data.isNotEmpty) {
      if ((state.addConstructBean.inverterPhoto ?? []).isEmpty) {
        state.addConstructBean.inverterPhoto = [];
      }
      for (var item in state.inverterPhotoController.data) {
        state.addConstructBean.inverterPhoto!.add(item.id ?? 0);
      }
    }
    if (check(flag)) {
      ResponseData response = await DioClient()
          .doPost(ApiConfig.saveConstruction, data: state.addConstructBean);
      if (response.isok) {
        if (response.code == "0") {
          LoadingUtils.showToast(response.msg ?? "提交成功");
          Get.put(UnconstructLogic()).callRefresh();
          HDNavigator.close(result: "refresh");
        } else {
          LoadingUtils.showToast(response.msg ?? "提交失败");
        }
      } else {
        LoadingUtils.showToast(response.msg ?? "提交失败");
      }
    }
  }

  bool check(int flag) {
    if(flag == 0){
      return true;
    }
    if ((state.addConstructBean.startDate ?? "").isEmpty) {
      LoadingUtils.showToast("请输入施工开始时间");
      return false;
    }
    if ((state.addConstructBean.endDate ?? "").isEmpty) {
      LoadingUtils.showToast("请输入施工结束时间");
      return false;
    }
    if ((state.addConstructBean.address ?? "").isEmpty) {
      LoadingUtils.showToast("请输入踏勘位置");
      return false;
    }
    if ((state.addConstructBean.safetyUser ?? "").isEmpty) {
      LoadingUtils.showToast("请输入安全员姓名");
      return false;
    }
    if ((state.addConstructBean.materialsEntrancePhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传材料进场照片");
      return false;
    }
    if ((state.addConstructBean.classMeetingPhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传早晚班会照片");
      return false;
    }
    if ((state.addConstructBean.safetyProtectionPhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传安全防护照片");
      return false;
    }
    if ((state.addConstructBean.stentInstallPhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传支架安装照片");
      return false;
    }
    if ((state.addConstructBean.stentInstallGround??[]).isEmpty) {
      LoadingUtils.showToast("请上传组件安装照片");
      return false;
    }
    if ((state.addConstructBean.inverterInstallGround??[]).isEmpty) {
      LoadingUtils.showToast("请上传逆变器、配电箱安装照片");
      return false;
    }
    if ((state.addConstructBean.stentInstallGround??[]).isEmpty) {
      LoadingUtils.showToast("请上传支架安装接地照片");
      return false;
    }
    if ((state.addConstructBean.inverterInstallGround??[]).isEmpty) {
      LoadingUtils.showToast("请上传逆变器、配电箱安装接地照片");
      return false;
    }
    if ((state.addConstructBean.cableLayingPhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传 电缆铺设照片");
      return false;
    }
    if ((state.addConstructBean.componentFinishPhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传组件安装完成航拍图");
      return false;
    }
    if ((state.addConstructBean.resistancePhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传电阻照片");
      return false;
    }
    if ((state.addConstructBean.voltagePhoto??[]).isEmpty) {
      LoadingUtils.showToast("请上传电压照片");
      return false;
    }
    return true;
  }

  Future<void> initImgUrl() async {
    if ((state.constructionLink.materialsEntrancePhoto ?? "").isNotEmpty) {
      state.mEntrancePhotoController.resetImages([]);
      List<ImagePickerData> materialsEntrancePhotoList = await DioClient()
          .requestImgUrl(state.constructionLink.materialsEntrancePhoto);
      state.mEntrancePhotoController.addImages(materialsEntrancePhotoList);
    }
    if ((state.constructionLink.classMeetingPhoto ?? "").isNotEmpty) {
      state.classMeetingPhotoController.resetImages([]);
      List<ImagePickerData> classMeetingPhotoData = await DioClient()
          .requestImgUrl(state.constructionLink.classMeetingPhoto);
      state.classMeetingPhotoController.addImages(classMeetingPhotoData);
    }
    if ((state.constructionLink.safetyProtectionPhoto ?? "").isNotEmpty) {
      state.safetyProtectionPhotoController.resetImages([]);
      List<ImagePickerData> panoramicData = await DioClient()
          .requestImgUrl(state.constructionLink.safetyProtectionPhoto);
      state.safetyProtectionPhotoController.addImages(panoramicData);
    }
    if ((state.constructionLink.stentInstallPhoto ?? "").isNotEmpty ) {
      state.stentInstallPhotoController.resetImages([]);
      List<ImagePickerData> stentInstallPhotoList = await DioClient()
          .requestImgUrl(state.constructionLink.stentInstallPhoto);
      state.stentInstallPhotoController.addImages(stentInstallPhotoList);
    }
    if ((state.constructionLink.componentInstallPhoto ?? "").isNotEmpty) {
      state.componentInstallPhotoController.resetImages([]);
      List<ImagePickerData> componentInstallPhotoList = await DioClient()
          .requestImgUrl(state.constructionLink.componentInstallPhoto);
      state.componentInstallPhotoController
          .addImages(componentInstallPhotoList);
    }
    if ((state.constructionLink.other ?? "").isNotEmpty) {
      state.rnOtherImgController.resetImages([]);
      List<ImagePickerData> otherData =
          await DioClient().requestImgUrl(state.constructionLink.other);
      state.rnOtherImgController.addImages(otherData);
    }
    if ((state.constructionLink.inverterInstallGround ?? "").isNotEmpty) {
      state.inverterInstallGroundController.resetImages([]);
      List<ImagePickerData> inverterInstallGroundList = await DioClient()
          .requestImgUrl(state.constructionLink.inverterInstallGround);
      state.inverterInstallGroundController
          .addImages(inverterInstallGroundList);
    }
    if ((state.constructionLink.inverterInstallPhoto ?? "").isNotEmpty) {
      state.inverterInstallPhotoController.resetImages([]);
      List<ImagePickerData> inverterInstallPhotoList = await DioClient()
          .requestImgUrl(state.constructionLink.inverterInstallPhoto);
      state.inverterInstallPhotoController.addImages(inverterInstallPhotoList);
    }
    if ((state.constructionLink.stentInstallGround ?? "").isNotEmpty) {
      state.stentInstallGroundController.resetImages([]);
      List<ImagePickerData> stentInstallGroundList = await DioClient()
          .requestImgUrl(state.constructionLink.stentInstallGround);
      state.stentInstallGroundController.addImages(stentInstallGroundList);
    }
    if ((state.constructionLink.cableLayingPhoto ?? "").isNotEmpty) {
      state.cableLayingPhotoController.resetImages([]);
      List<ImagePickerData> cableLayingPhotoList = await DioClient()
          .requestImgUrl(state.constructionLink.cableLayingPhoto);
      state.cableLayingPhotoController.addImages(cableLayingPhotoList);
    }
    if ((state.constructionLink.componentFinishPhoto ?? "").isNotEmpty) {
      state.componentFinishPhotoController.resetImages([]);
      List<ImagePickerData> componentFinishPhoto = await DioClient()
          .requestImgUrl(state.constructionLink.componentFinishPhoto);
      state.componentFinishPhotoController.addImages(componentFinishPhoto);
    }
    if ((state.constructionLink.voltagePhoto ?? "").isNotEmpty) {
      state.voltagePhotoController.resetImages([]);
      List<ImagePickerData> voltagePhoto =
          await DioClient().requestImgUrl(state.constructionLink.voltagePhoto);
      state.voltagePhotoController.addImages(voltagePhoto);
    }
    if ((state.constructionLink.resistancePhoto ?? "").isNotEmpty) {
      state.resistancePhotoController.resetImages([]);
      List<ImagePickerData> resistancePhoto = await DioClient()
          .requestImgUrl(state.constructionLink.resistancePhoto);
      state.resistancePhotoController.addImages(resistancePhoto);
    }
    if ((state.constructionLink.inverterPhoto ?? "").isNotEmpty) {
      state.inverterPhotoController.resetImages([]);
      List<ImagePickerData> inverterPhotoList =
          await DioClient().requestImgUrl(state.constructionLink.inverterPhoto);
      state.inverterPhotoController.addImages(inverterPhotoList);
    }
    update();
  }

  Future<void> setData(ConstructionLink? tempData) async {
    if (tempData != null) {
      state.constructionLink = tempData;
      await initImgUrl();
      Get.find<RefreshTabLogic>(tag: "4").refreshTabBar();
    }
  }

  Future<void> jumpScanPage() async {
    var args = {"pageData": state.constructionLink.componentNumber,"isReadOnly":state.isReadOnly};
    var result = await HDNavigator.openToName(
        RouteConfig.scanPage, arguments: args);
    if(result != null){
        state.constructionLink.componentNumber = result ;
        Get.find<RefreshTabLogic>(tag: "4").refreshTabBar();
    }
  }

  @override
  void dispose() {
    LocationGaode().dispose();
  }
}
